<?php
	class CommentAction extends BaseAction{
		
		//新增评论
		public function insert(){
			if($_SESSION['verify'] != md5($_POST['verify'])) {
				$this->error('验证码错误！');
				exit();
			}
			$comment = D('Comment');
			if($comment->create()){
				if($comment->add()){
					$this->success('评论成功！');
				}else{
					$this->error('发表评论失败！');
				}
			}else{
				$this->error($comment->getError());
			}
		}
		
		//显示评论列表
		public function show(){
			if(empty($_GET['aid'])){
				$this->error('请选择要查看评论列表的文章！');
				exit();
			}
			$art = M('Article');
			$artData = $art->field('id,info,title')->getById($_GET['aid']);
			$this->assign('art',$artData);
			if(!$artData){
				$this->error('请确保要查看文章的ID正确！');
				exit();
			}
			$comment = M('Comment');
			//获得最火三条评论,标准：支持和反对之和大于0的显示
			$topThreeCom = $comment->field('zgcms_comment.id id,zgcms_comment.date date,content,manner,support,oppose,user,face,uid')
								->join('zgcms_user on zgcms_comment.uid = zgcms_user.id')
								->where('zgcms_comment.state = 1 and cast(support - oppose as signed) > 0 and aid = '.$artData['id'])
								->order('support - oppose desc')
								->limit(3)
								->select();
			//因为support 和 oppose都是无符号整型，做减法时，要用cast(support - oppose as signed)强制类型转换为有符号整型。
			$this->assign('topThreeCom',$topThreeCom);
			
			//获得评论列表
			import("ORG.Util.Page");
			$count = $comment->where('state = 1 and aid = '.$artData['id'])
							->count();
			$page = new Page($count,c('PAGESIZE'));
			$show = $page->show();
			$comData = $comment->limit($page->firstRow.','.$page->listRows)
							->field('zgcms_comment.id id,zgcms_comment.date date,content,manner,support,oppose,user,face,uid')
							->join('zgcms_user on zgcms_comment.uid = zgcms_user.id')
							->where('zgcms_comment.state = 1 and aid = '.$artData['id'])
							->order('date desc')
							->select();
			

			$this->assign('page',$show);
			$this->assign('comData',$comData);
			
			//获取热评文档总排行，二十篇文章，按评论量大小排序
			$topTwentyArt = $art->field('id,title')
								->order("(select count(*) from zgcms_comment where aid = zgcms_article.id and state = 1) desc")
								->limit(20)
								->select();
			$this->assign('topTwentyArt',$topTwentyArt);
			
			$this->display();
		}
		
		//支持数加1
		public function support(){
			if(empty($_GET['id'])){
				$this->error('请选择要支持的评论！');
				exit();
			}
			$comment = M('Comment');
			$comData = $comment->getById($_GET['id']);
			if(!$comData){
				$this->error('请确保要支持的评论ID正确！');
				exit();
			}
			$comment->where('id = '.$_GET['id'])->setInc('support',1);
			$this->success('支持成功！');
		}
		
		//反对数加1
		public function oppose(){
			if(empty($_GET['id'])){
				$this->error('请选择要反对的评论！');
				exit();
			}
			$comment = M('Comment');
			$comData = $comment->getById($_GET['id']);
			if(!$comData){
				$this->error('请确保要反对的评论ID正确！');
				exit();
			}
			$comment->where('id = '.$_GET['id'])->setInc('oppose',1);
			$this->success('反对成功！');
		}
	}
?>